﻿package yii.db{

	import yii.*;
	import yii.db.*;
	import phpas3.*;


	public class CDbMigration{

	private var _db:CDbConnection;





	/**
	 * Returns the currently active database connection+
	 * By default, the 'db' application component will be returned and activated+
	 * You can call {@link setDbConnection} to switch to a different database connection+
	 * Methods such as {@link insert}, {@link createTable} will use this database connection
	 * to perform DB queries+
	 * @return CDbConnection the currently active database connection
	 */
	public function getDbConnection():CDbConnection{
		if(this._db===null){
			this._db = Yii.app().getComponent('db');
			if(!this._db is CDbConnection)
				throw new Error( "The 「db」 application component must be configured to be a CDbConnection object." );
		}
		return this._db;
	}



	/**
	 * Builds and executes a SQL statement for creating a new DB table+
	 *
	 * The columns in the new  table should be specified as name-definition pairs (e+g+ 'name'=>'string'),
	 * where name stands for a column name which will be properly quoted by the method, and definition
	 * stands for the column type which can contain an abstract DB type+
	 * The {@link getColumnType} method will be invoked to convert any abstract type into a physical one+
	 *
	 * If a column is specified with definition only (e+g+ 'PRIMARY KEY (name, type)'), it will be directly
	 * inserted into the generated SQL+
	 *
	 * @param string $table the name of the table to be created+ The name will be properly quoted by the method+
	 * @param array $columns the columns (name=>definition) in the new table+
	 * @param string $options additional SQL fragment that will be appended to the generated SQL+
	 */
	public function createTable( $table:String, $columns:Object, $options:String=null ):void{
		Yii.log( "    > create table "+ $table +" ..." );
		var $time:int = microtime(true);
		this.getDbConnection().createCommand().createTable($table, $columns, $options);
		Yii.log( " done (time: "+ sprintf('%+3f', microtime(true)-$time) +"s)\n" );
	}




	/**
	 * Builds and executes a SQL statement for creating a new index.
	 * @param string $name the name of the index. The name will be properly quoted by the method.
	 * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method.
	 * @param string $column the column(s) that should be included in the index. If there are multiple columns, please separate them
	 * by commas. The column names will be properly quoted by the method.
	 * @param boolean $unique whether to add UNIQUE constraint on the created index.
	 */
	public function createIndex( $name:String, $table:String, $column:String, $unique:Boolean=false ):void{
		Yii.log( "    > create"+ ($unique ? ' unique':'') +" index "+ $name +" on "+ $table +" ("+ $column +") ..." );
		var $time:int = microtime(true);
		this.getDbConnection().createCommand().createIndex( $name, $table, $column, $unique );
		Yii.log( " done (time: "+ sprintf('%.3f', microtime(true)-$time) +"s)\n" );
	}


























	}
}